package advanced.io;

import java.io.*;

/**
 * 打印流  PrintStream
 * <p>
 * System.out
 */
public class L_PrintStream {
    public static void main(String[] args) throws FileNotFoundException {
        PrintStream ps = System.out;
        ps.println("打印流");
        ps.println(true);
        ps.flush();
        //将输出打印到文件
        String dest = "C:\\Users\\DMLYS\\Documents\\prepare_for_work\\pfw\\java_l\\src\\main\\java\\advanced\\io" + "/print.txt";
        ps = new PrintStream(new BufferedOutputStream(new FileOutputStream(dest, false)), true);
        ps.println("打印流");
        ps.println(true);

        //重定向输入出端,  System.out.println 不在输出到控制台,而是ps指定的路径
        System.setOut(ps);
        System.out.println("output direction has changed");

        //重定向回控制台
        System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), true));
        System.out.println("i'm back");
        if (ps != null)
            ps.close();
    }
}
